package com.meihua.baoleyi.adapters;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.bean.BillBean;
import com.meihua.baoleyi.ui.MyWalletBillActivity;
import com.meihua.baoleyi.utils.MyDateUtils;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ${zhangyuanchao} on 2017/10/26.
 */
public class CommonAdapter extends RecyclerView.Adapter<CommonAdapter.IViewHolder> {
    private Activity activity;
    private List<BillBean.DatasBean> mData;

    public CommonAdapter(MyWalletBillActivity myWalletBillActivity, List<BillBean.DatasBean> Data) {
        activity = myWalletBillActivity;
        mData = Data;
    }

    @Override
    public IViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new IViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bill_list, parent, false));
    }

    @Override
    public void onBindViewHolder(IViewHolder holder, final int position) {
        holder.setData(mData.get(position));
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class IViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title_time)
        TextView tvTitleTime;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.tv_time)
        TextView tvTime;
        @Bind(R.id.iv_img)
        ImageView ivImg;
        @Bind(R.id.tv_money)
        TextView tvMoney;
        @Bind(R.id.tv_bill_sources)
        TextView tvBillSources;

        public IViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(BillBean.DatasBean datasBean) {
            tvBillSources.setText(datasBean.types);
            tvMoney.setText(datasBean.goods_price);
            tvBillSources.setText(datasBean.types);
            tvTime.setText(datasBean.create_time);
            tvDate.setText(datasBean.week);
            if (isFirstTime(MyDateUtils.getInstance().getTime(datasBean.create_time), getLayoutPosition())) {
                tvTitleTime.setVisibility(View.VISIBLE);
                tvTitleTime.setText(MyDateUtils.getInstance().getTime(datasBean.create_time));
            } else {
                tvTitleTime.setVisibility(View.GONE);
            }
           itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }
    }

    private boolean isFirstTime(String time, int position) {
        for (int i = 0; i < mData.size(); i++) {
            if (MyDateUtils.getInstance().getTime(mData.get(i).create_time).equals(time)) {
                if (i == position) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    static class ViewHolder {

    }
}
